package com.sinping.qh.api.admin.lab;

import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.MediaTypes;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.admin.lab.LabInspectItemDto;
import com.sinping.qh.dto.admin.lab.LabInspectItemRelationDto;
import com.sinping.qh.service.admin.lab.ILabInspectItemService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author jiahuaiqing
 * @create 2018/6/1
 */
@Api(value = "admin检查项管理(已废弃)", description = "admin检查项管理(已废弃)")
@RestController
@RequestMapping(name = "admin检查项管理(已废弃)", value = Const.ADMIN_URI + "inspectitem")
@Slf4j
@Deprecated
public class LabInspectItemEndPoint {

    @Autowired
    ILabInspectItemService iLabInspectItemService;


    @ApiOperation(httpMethod = "POST", value = "添加一级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.POST, name = "添加一级检查项", value = "/one")
    public ResDto addInspectItemOne(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"xxxxxx\",\"item_subcategory\":\"xxxxxx\",<Br>\"item_category\":\"xxxx\"}")
                                    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "检查项名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemCategory())) {
                return new ResDto(Const.PARAM_ERROR, "类型不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemSubcategory())) {
                return new ResDto(Const.PARAM_ERROR, "分类不为空");
            }
            ResDto resDto = iLabInspectItemService.addInspectItemOne(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 实验室检查项 二级
     */
    @ApiOperation(httpMethod = "POST", value = "添加二级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.POST, name = "添加二级检查项", value = "/two")
    public ResDto addInspectItemTwo(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"xxxxxx\",<Br>\"id\":\"xxxxxx\"}")
                                    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto inspectItemTwoDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (inspectItemTwoDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(inspectItemTwoDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "名称不为空");
            }
            if (!StringUtils.isNotBlank(inspectItemTwoDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "上级id不为空");
            }
            ResDto resDto = iLabInspectItemService.addInspectItemTwo(request,inspectItemTwoDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 实验室检查项 三级 隐患描述
     */
    @ApiOperation(httpMethod = "POST", value = "添加三级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.POST, name = "添加三级检查项", value = "/three")
    public ResDto addInspectItemThree(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"xxxxxx\",\"id\":\"xxxxxx\",,\"item_id_two\":\"xxxxxx\"}")
                                      @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "隐患描述信息不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "一级id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemIdTwo())) {
                return new ResDto(Const.PARAM_ERROR, "二级id不为空");
            }
            ResDto resDto = iLabInspectItemService.addInspectItemThree(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    @ApiOperation(httpMethod = "PUT", value = "修改一级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.PUT, name = "修改一级检查项", value = "/one")
    public ResDto updateInspectItemOne(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"id\":\"xxxxxx\",\"item_name\":\"xxxxxx\",<Br>\"item_subcategory\":\"xxxxxx\",<Br>\"item_category\":\"xxxx\"}")
                                       @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "检查项名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemCategory())) {
                return new ResDto(Const.PARAM_ERROR, "类型不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemSubcategory())) {
                return new ResDto(Const.PARAM_ERROR, "分类不为空");
            }
            ResDto resDto = iLabInspectItemService.updateInspectItemOne(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    @ApiOperation(httpMethod = "PUT", value = "修改二级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.PUT, name = "修改二级检查项", value = "/two")
    public ResDto updateInspectItemTwo(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"id\":\"xxxxxx\",\"item_name\":\"xxxxxx\",<Br>\"item_id_two\":\"xxxxxx\"}")
                                       @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "检查项名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemIdTwo())) {
                return new ResDto(Const.PARAM_ERROR, "二级检查项id不为空");
            }
            ResDto resDto = iLabInspectItemService.updateInspectItemTwo(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    @ApiOperation(httpMethod = "PUT", value = "修改三级检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.PUT, name = "修改三级检查项", value = "/three")
    public ResDto updateInspectItemThree(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"id\":\"xxxxxx\",\"item_name\":\"xxxxxx\",<Br>\"item_id_two\":\"xxxxxx\",<Br>\"item_id_three\":\"xxxx\"}")
                                         @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "检查项名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemIdTwo())) {
                return new ResDto(Const.PARAM_ERROR, "二级检查项id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemIdThree())) {
                return new ResDto(Const.PARAM_ERROR, "三级检查项id不为空");
            }
            ResDto resDto = iLabInspectItemService.updateInspectItemThree(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 获取检查项列表
     * item_desc 查询第三级的隐患描述,只要它有值,就直接查三级啦
     * {} 全部
     * 不能查二级类
     *
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取检查项列表", consumes = MediaTypes.JSON,notes = "查询全部{}<Br>不能查二级")
    @RequestMapping(method = RequestMethod.GET, name = "获取检查项列表", value = "/list")
    public ResDto getInspectItem(@ApiParam(name = "msg", value = "{\"item_name\":\"xxxxxx\",\"item_subcategory\":\"xxxxxx\",\"item_desc\":\"xxxxxx\"}")
                                 @RequestParam(required = true) String msg) {

        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (labInspectItemDto == null) {
                log.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            ResDto resDto = iLabInspectItemService.getInspectItemList(labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 删除检查项
     * 三个id，item_id_two，item_id_three不为空直接删除三级数据 {"id":"xxxxxx","item_id_two":"xxxxxx","item_id_three":"xxxxxx"}
     * 前二个id，item_id_two不为空直接删除二级数据 {"id":"xxxxxx","item_id_two":"xxxxxx"}
     * 前一个id不为空直接删除一级数据 {"id":"xxxxxx"}
     *
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除检查项", consumes = MediaTypes.JSON,notes = "逻辑删除一级id,item_id_two<br>逻辑删除一级id,item_id_two,item_id_three")
    @RequestMapping(method = RequestMethod.DELETE, name = "删除检查项", value = "/delete")
    public ResDto deleteLabInspectItemById(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"id\":\"xxxxxx\",\"item_id_two\":\"xxxxxx\",\"item_id_three\":\"xxxxxx\"}")
                                           @RequestParam(required = true) String msg) {
        try {
            LabInspectItemDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemDto.class);
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            ResDto resDto = iLabInspectItemService.deleteLabInspectItemById(request,labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 获取危险源隐患库
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取危险源隐患库", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.GET, name = "获取危险源隐患库", value = "/dangerlist")
    public ResDto getInspectItemListOfHiddenDanger() {
        ResDto resDto = iLabInspectItemService.getInspectItemList(new String[]{"2"});
        return resDto;
    }

    /**
     * 添加危险源隐患库
     * {"lab_id":"xxxxxx","item_id_one":"xxxxxx"} 配置一级
     * {lab_id":"xxxxxx","item_id_one":"xxxxxx","item_id_two":"xxxxxx"} 配置二级
     *
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "添加危险源隐患库", consumes = MediaTypes.JSON,notes = "添加一级检查项lab_id,item_id_one必传值<Br>添加二级检查项lab_id,item_id_one,item_id_two必传值")
    @RequestMapping(method = RequestMethod.POST, name = "添加危险源隐患库", value = "/config")
    public ResDto addConfigInspectItem(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"lab_id\":\"xxxxxx\",\"item_id_one\":\"xxxxxx\",\"item_id_two\":\"xxxxxx\"}")
                                       @RequestParam(required = true) String msg) {
//        try {
//            LabInspectItemRelationDto labInspectItemRelationDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemRelationDto.class);
//            if (!StringUtils.isNotBlank(labInspectItemRelationDto.getLabId())) {
//                return new ResDto(Const.PARAM_ERROR, "实验室id不为空");
//            }
//            if (!StringUtils.isNotBlank(labInspectItemRelationDto.getItemIdOne())) {
//                return new ResDto(Const.PARAM_ERROR, "检查项一级id不为空");
//            }
//            ResDto resDto = iLabInspectItemService.addConfigInspectItem(request,labInspectItemRelationDto);
//            return resDto;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
//        }
        return null;
    }

    /**
     * 删除已经配置的实验室检查项
     *
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除已经配置的危险源隐患", consumes = MediaTypes.JSON,notes = "删除一级检查项lab_id,item_id_one必传值<Br>删除二级检查项lab_id,item_id_one,item_id_two必传值")
    @RequestMapping(method = RequestMethod.DELETE, name = "删除已经配置的危险源隐患", value = "/config")
    public ResDto deleteConfigInspectItem(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"lab_id\":\"xxxxxx\",\"item_id_one\":\"xxxxxx\",\"item_id_two\":\"xxxxxx\"}")
                                          @RequestParam(required = true) String msg) {
//        try {
//            LabInspectItemRelationDto labInspectItemRelationDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemRelationDto.class);
//            if (!StringUtils.isNotBlank(labInspectItemRelationDto.getLabId())) {
//                return new ResDto(Const.PARAM_ERROR, "实验室id不为空");
//            }
//            if (!StringUtils.isNotBlank(labInspectItemRelationDto.getItemIdOne())) {
//                return new ResDto(Const.PARAM_ERROR, "检查项一级id不为空");
//            }
//            ResDto resDto = iLabInspectItemService.deleteConfigInspectItem(request,labInspectItemRelationDto);
//            return resDto;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
//        }
        return null;
    }

}
